/**
 * @author Tibor Kiray
 */
Ext.namespace('Softab');

Softab.GetEntityGrid = function(node, event){
    if (node.id != 0 && !node.isLeaf()) {
        if (Softab.TabsPanel.findById(node.id + "_EditorGrid") == null) {
            var storeDefinition;
            Ext.Ajax.request({
                url: 'server/EntityGrid.php?definition=true&__id=' + node.id,
                success: function(response){
                    r = response.responseText;
                    var gridDefinition = eval(r);
                    StoreDefinition = eval(gridDefinition.Store);
                    ColumnsModel = eval(gridDefinition.ColumnModel);
                    //alert(EntitiesDefinition);
                    EntityDataStore = new Ext.data.Store({
                        id: node.id + 'EntityDataStore',
                        proxy: new Ext.data.HttpProxy({
                            url: 'server/EntityGrid.php?definition=false&__id=' + node.id, // File to connect to
                            method: 'POST'
                        }),
                        baseParams: {
                            task: "LISTING"
                        }, // this parameter asks for listing
                        reader: new Ext.data.JsonReader({ // we tell the datastore where to get his data from
                            root: 'results',
                            totalProperty: 'total',
                            id: 'id'
                        }, StoreDefinition),
                        sortInfo: {
                            field: 'ID',
                            direction: "ASC"
                        }
                    });
                    EntityColumnModel = new Ext.grid.ColumnModel(ColumnsModel);
                    EntityColumnModel.defaultSortable = true;
                    EntityListingEditorGrid = new Ext.grid.EditorGridPanel({
                        id: node.id + "_EditorGrid",
                        title: node.text,
                        store: EntityDataStore, // the datastore is defined here
                        cm: EntityColumnModel, // the columnmodel is defined here
                        enableColLock: false,
                        clicksToEdit: 1,
                        tbar: [new Ext.Button({
                            text: "Add Record",
                            iconCls: 'btn-add-record',
							handler:function(){Softab.EntityAddRecordHandler(node.id)}
                        }), '-', new Ext.app.SearchField({
                            store: EntityDataStore,
                            params: {
                                start: 0,
                                limit: 15
                            },
                            width: 120
                        })],
                        bbar: new Ext.PagingToolbar({
                            pageSize: 15,
                            store: EntityDataStore,
                            displayInfo: true
                        }),
                        selModel: new Ext.grid.RowSelectionModel({
                            singleSelect: false
                        })
                    });
                    EntityListingEditorGrid.on('afteredit', Softab.ComitInlineEdit);
                    EntityListingEditorGrid.on("rowcontextmenu", Softab.RenderEntityGridMenu);
					//EntityListingEditorGrid.on("contextmenu", Softab.RenderEntityGridMenu);
                    
                    EntityDataStore.load({
                        params: {
                            start: 0,
                            limit: 15
                        }
                    });
                    EntityListingEditorGrid.closable = true;
                    Softab.TabsPanel.add(EntityListingEditorGrid);
                    Softab.TabsPanel.setActiveTab(EntityListingEditorGrid.id);
                }
            });
        }
        else {
            grid = Softab.TabsPanel.getComponent(node.id + "_EditorGrid");
            grid.store.load({
                params: {
                    start: 0,
                    limit: 15
                }
            });
            grid.render();
            Softab.TabsPanel.setActiveTab(node.id + "_EditorGrid");
        }
    }
}


Softab.ComitInlineEdit = function(){
	
}
